#include <stdio.h>
#include <stdlib.h>
#define MAX 4

typedef struct{
    int arr[MAX];
    int top; // 栈顶指针
} Stack;

int pop(Stack *stack){
    if (stack->top>-1){
        stack->top--;
        return stack->arr[stack->top+1];
    }else{
        printf("Stack is emply!\n");
    }
}

void push(Stack *stack, int value){
    if (stack->top<MAX-1){
        stack->top++;
        stack->arr[stack->top] = value;
    }else{
        printf("Stack is full!\n");
    }
}

void peek(Stack *stack){
    if (stack->top>-1){
        printf("top: %d\n", stack->arr[stack->top]);
    }else{
        printf("Stack is empty!\n");
    }
}

Stack *createstack(){
    Stack *stack = (Stack*)malloc(sizeof(Stack));
    stack->top = -1;
    return stack;
    // 也可以用静态内存分配办法，后面配合free()释放内存
    // static Stack stack;
    // stack.top = -1;
    // return &stack;
}


int main(){
    Stack *stack = createstack();
    int p = pop(stack);
    push(stack,10);
    push(stack,3);
    push(stack,55);
    push(stack,12321);

    peek(stack);
    printf("%d\n",pop(stack));
    peek(stack);
    printf("%d\n",pop(stack));
    peek(stack);
    printf("%d\n",pop(stack));
    peek(stack);
    printf("%d\n",pop(stack));
    

    return 0;
}